package groovy.etl

import groovy.sql.Sql
import org.h2.jdbcx.JdbcDataSource
import org.junit.BeforeClass
import org.junit.Test

/**
 * Created with IntelliJ IDEA.
 * User: tbarker
 * Date: 5/6/12
 * Time: 5:25 PM
 * To change this template use File | Settings | File Templates.
 */
class EtlTest {

    def static dataSource
    def static tempFile = File.createTempFile("h2_test", ".db")
    def static url = "jdbc:h2:file:${tempFile.path}"
    def static sql

    @BeforeClass
    static void setupTests() {
        tempFile.deleteOnExit()
        dataSource = new JdbcDataSource()
        dataSource.URL = url
        sql = new Sql(dataSource)
    }

    @Test
    void simpleQuery() {
        Etl.etl {
            connection(url: url, driver: "h2")
            script(
                """
                    create table FOO (
                        bar int,
                    );
                    create table BAR (
                        bar int,
                    );
                    insert into foo values(1);
                    insert into foo values(5);
                """
            )
            query("select * from foo") {

                script(
                    """
                        insert into bar values (?bar)
                    """
                )
            }
        }

        assert 2 == sql.firstRow("select count(*) as total from foo").total
        assert 2 == sql.firstRow("select count(*) as total from bar").total
        sql.execute("drop table foo")
        sql.execute("drop table bar")
    }

    @Test
    void testSimpleQueryWithScripting() {
        Etl.etl {
            connection(id: "embedded", url: url, driver: "h2",
                """
                    statement.batchSize=100
                    statement.fetchSize=100
                """
            )
            connection(id: "groovy", driver: "script",
                "language=groovy"
            )
            script("connection-id": "embedded",
                """
                    create table foo (
                        bar int
                    );
                    insert into foo values (12);
                    insert into foo values (134);
                    insert into foo values (2);
                    insert into foo values (56);
                    insert into foo values (12);
                    create table bar (
                        timesten int
                    );
                """
            )
            query("connection-id": "embedded",
                """
                    select bar from foo
                """){
                    query("connection-id": "groovy",
                        """
                            def closure = groovy.etl.EtlTestHelper.timesTen
                            closure.delegate = this
                            foobar = closure.call()
                            query.next()
                        """
                    ) {
                        script("connection-id": "embedded",
                            """
                                insert into bar values (?foobar)
                            """
                        )
                    }
            }
        }

        assert 5 == sql.firstRow("select count(*) as total from bar").total

    }
}

class EtlTestHelper {
    def static timesTen = {
        BAR * 10
    }
}